package com.wang.constants;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wanglei
 * @desc
 * @date 2018/12/6
 * @see
 * @since 1.0
 */
@Slf4j
public class SystemParam {

    public static String url(String code) {
        long millis = System.currentTimeMillis();
        String url = "http://fundgz.1234567.com.cn/js/" + code + ".js?rt=" + millis;
        return url;
    }

    public static String tianUrl(String code) {
        String url = String.format("http://fund.eastmoney.com/%s.html", code);
        return url;
    }

    public static BigDecimal getHistoricalData(String code, Integer pageIndex) throws IOException {
        String url = "http://fund.eastmoney.com/f10/F10DataApi.aspx?";
        Map<String, Object> map = new HashMap<>();
        map.put("type", "lsjz");
        map.put("per", "49");
        map.put("page", pageIndex++);

        String params = HttpUtil.toParams(map);


        Document dom = Jsoup.connect(url + params)
//                .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:49.0) Gecko/20100101 Firefox/49.0")
//                .header("Connection", "close")
//                .timeout(8000)
                .get();

        Elements trlist = dom.body().select("tbody").select("tr");
        for (Element element : trlist) {
            log.info("{}  {}", element.select("td").get(0).html(), element.select("td").get(1).html());
        }

        return null;
    }

    public static void main(String[] args) throws IOException {

        getHistoricalData("001630",1);
    }

}
